package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
)

func main() {
	lines, err := GetStrings("../names.txt")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(lines)
	var names []string
	var counts []int
	for _, line := range lines {
		matched := false
		for index, name := range names {
			if name == line {
				counts[index]++
				matched = true
			}
		}
		if !matched {
			names = append(names, line)
			counts = append(counts, 1)
		}
	}
	for index, name := range names {
		fmt.Printf("%s: %d\n", name, counts[index])
	}
}

func GetStrings(fileName string) ([]string, error) {
	var lines []string
	file, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	err = file.Close()
	if err != nil {
		return nil, err
	}
	if scanner.Err() != nil {
		return nil, scanner.Err()
	}
	return lines, nil
}
